package cn.ava.org.controller;

import cn.ava.basic.utils.AjaxResult;
import cn.ava.basic.utils.PageList;
import cn.ava.org.domain.Employee;
import cn.ava.org.query.EmployeeQuery;
import cn.ava.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: EmployeeController
 * @Description: 员工控制层接口
 * @Author: Avavaava
 * @Date: 2022/6/24 15:07
 * @version: 23.53
 *
 */
@Api(value = "员工Controller层",description = "员工列表CRUD模块")
@RestController
@RequestMapping("/emp")
public class EmployeeController {

    // 实例化接口对象
    @Autowired
    private IEmployeeService employeeService;

    /** 
    * @Title: list
    * @Description: 查询所有员工列表
    * @Author: Avavaava
    * @Version: 23.53
    * @Date:  2022/6/24 16:27
    * @param: 
    * @return: 
    **/
    @GetMapping
    @ApiOperation(value = "查询所有员工")
    public List<Employee> list() {
        return employeeService.loadAll();
    }
    
    /** 
    * @Title: getById
    * @Description: 根据员工id查询员工
    * @Author: Avavaava
    * @Version: 23.53
    * @Date:  2022/6/24 16:28
    * @param: 
    * @return: 
    **/
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询员工数据")
    public Employee getById(@PathVariable("id") Long id) {
        return employeeService.getById(id);
    }

    /** 
    * @Title: delete
    * @Description: 根据员工id删除员工列表数据
    * @Author: Avavaava
    * @Version: 23.53
    * @Date:  2022/6/24 16:28
    * @param: 
    * @return: 
    **/
    @DeleteMapping("/{id}")
    public AjaxResult delete(@ApiParam(value = "主键",readOnly = true) @PathVariable("id") Long id) {
        try {
            employeeService.delete(id);
           return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }

    /**
    * @Title: addOrUpt
    * @Description: 新增或修改员工数据
    * @Author: Avavaava
    * @Version: 23.53
    * @Date:  2022/6/24 16:29
    * @param:
    * @return:
    **/
    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Employee employee) {

        try {
            if (employee.getId() == null) {
                employeeService.add(employee);
            }else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }

    /**
    * @Title: queryDept
    * @Description: 高级查询员工列表
    * @Author: Avavaava
    * @Version: 23.53
    * @Date:  2022/6/24 16:29
    * @param:
    * @return:
    **/
    @PostMapping
    public PageList<Employee> queryEmp(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryData(employeeQuery);
    }

    /**
     * @Title: batchDelete
     * @Description: 批量删除数据
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/6/25 11:19
     * @param:
     * @return:
     */
    @PatchMapping
    public AjaxResult batchDelete( @RequestBody Long[] id) {
        try {
            employeeService.batchDelete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }


    @GetMapping("/shops/{shopId}")
    public List<Employee> getShops(@PathVariable("shopId") Long id) {
        return employeeService.getByShopId(id);
    }


}
